#ifndef _gsm_h
#define _gsm_h
#define AtRevBufferMaxLength 1024
extern unsigned char save;
extern unsigned char CSQ_Num;
extern unsigned char UdpSim800BUF[1024];
extern unsigned char UdpSim800COUNT;
typedef enum
{
AT_NULL=0,
	AT_ATE,
	AT_CPIN,
	AT_CREG,
	AT_CGREG,
	AT_CSQ,
        AT_CENG,
        AT_CGATT,
        AT_IPMUX,        
        AT_CSTT,
        AT_CIICR,
        AT_CIFSR,
	AT_CREATSOCKET1,
	AT_CLOSESOCKET1,
	AT_SENDSOCKET1,
	AT_TCPSendData,
        AT_CREATSOCKET2,
	AT_CLOSESOCKET2,
	AT_SENDSOCKET2,
	AT_UDPSendData
	
}ATType;
typedef enum
{
  ERR=0,
  OK,
  WITE
}StatusDef;
typedef struct
{
  StatusDef SIM;
  StatusDef CREG;
  StatusDef GREG;
  StatusDef PDP;
  StatusDef GPRS;
  unsigned char CSQ;
}GSMTypeDef;
typedef void (*gsmAtFunctionHandler)(unsigned char * par,unsigned short len);

typedef struct
{
	ATType	gsmAtFunctionCode;
	gsmAtFunctionHandler	AtHandler;
	unsigned char *str;
}AtFunctionHandler;
typedef struct
{
	ATType	gsmAtFunctionCode;	
	gsmAtFunctionHandler	AtHandler;
	unsigned char *str;
}AtRevFunctionHandler;
typedef enum
{
  AtWiteNull=0,
  AtWiteSend,
  AtWiteRev
}AtCmdStatus;
typedef enum
{
  AtCmdListFull=1,
  AtCmdListOK,
  AtCmdListEmpty,
}AtCmdListResult;

typedef struct
{
  ATType  AtNow;
  AtCmdStatus AtCmd;
  unsigned short AtTimeOut;
  unsigned char AtTimeOutCount;
  unsigned char AtRetry;
  unsigned char AtRetryCount;
  unsigned char AtRevFlag;
  unsigned char AtRevBuffer[AtRevBufferMaxLength];
  unsigned short AtRevBufferLength;
}AtStatus;
typedef struct
{
   ATType AtCmd;
   unsigned short TimeOut;
   unsigned char AtCmdBuffer[256];
   unsigned char AtCmdBufferLength;
}AtCmdListDef;
typedef enum
{
  At_OK=1,
  At_ERR,
  At_Connect,
}AtResult;
typedef enum
{
  TCP=1,
  UDP
}SocketTypeDef;
#define SocketMaxBufferLength 1024
typedef struct
{
  SocketTypeDef Type;
  unsigned char IP[16];
  unsigned char Port[6];
	unsigned char ErrCount;
	unsigned int PauseCount;
  StatusDef Connect;
  unsigned char DataRevBuffer[SocketMaxBufferLength];
  unsigned short DataRevBufferLength;
  unsigned char (*RevDataHandler)();
  unsigned char DataSendBuffer[SocketMaxBufferLength];
  unsigned short DataSendBufferLength;
	unsigned short RestartOutTime;
}SocketDef;

extern SocketDef USER_Socket;
extern SocketDef CLOUDE_Socket;
extern GSMTypeDef SIM800;
extern AtStatus SensorStatus;
extern AtStatus atStatus;;
void GsmAtStart(void);
void GsmMain(void);
void PauseAt(void);
void AtListStart(void);
AtCmdListResult AtCmdPostList(ATType cmd,unsigned short timeout,unsigned char *at,unsigned char atlen);
AtCmdListDef *AtCmdPendList(void);
void Res_Startwork(void);
void AtStatusChange(ATType At, unsigned short Timeout);
#endif
